﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gfw.Infrastructure.Stock.Facilities;
using Gfw.Repositories.Stock;
using Gfw.Repository;

namespace StockWcf.Manager
{
    public static class AutoSellManager
    {
        internal static void AutoSell(decimal price)
        {
            //首先查找下一次卖出价格是当前价格的
            UnitOfWork work = new UnitOfWork();
            try
            {
                UserAccountRepository userRep = new UserAccountRepository();
                var aclist = userRep.GetByNextAutoSellPrice(price, work);
                foreach (var ac in aclist)
                {
                    try
                    {
                        SellTradeFacility stf = new SellTradeFacility(ac, Math.Floor(ac.StockCount*0.7M), price,Gfw.Infrastructure.Stock.Enums.ForceTradeTypeEnum.ForceSell);
                        stf.Execute(work);
                        ac.LastAutoSellPrice = price;
                        ac.NextAutoSellPrice = price + BargainFacility.EnforceSell;
                        userRep.Update(ac);
                    }
                    catch (Exception exp)
                    {

                    }
                }
                work.Commit();
            }
            catch(Exception exp)
            {
                work.Rollback();
            }
        }
    }
}